package com.sdcqjy.property.lawRegulations;

import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.nineoldandroids.animation.ValueAnimator;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sdcqjy.mylibrary.KeyBoardUtils;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.LawRegulationsAdapter;
import com.sdcqjy.property.base.BaseFragment;
import com.sdcqjy.property.mode.LawRegulationsDetailsMode;
import com.sdcqjy.property.mode.LawRegulationsMode;
import com.sdcqjy.property.presenter.LawRegulationsPresenter;
import com.sdcqjy.property.presenter.contract.LawRegulationsContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/7/8.
 * 功能：
 */

public class LawRegulationsListFragment extends BaseFragment<LawRegulationsPresenter> implements
        LawRegulationsContract.View, OnRefreshListener, LawRegulationsAdapter.OnAdapterClickListener {
    @BindView(R.id.editSearch)
    EditText editSearch;
    @BindView(R.id.imgClear)
    ImageView imgClear;
    @BindView(R.id.layoutSearch)
    FrameLayout layoutSearch;
    @BindView(R.id.listView)
    ExpandableListView listView;
    @BindView(R.id.smartLayout)
    SmartRefreshLayout smartLayout;
    private boolean isOpen = false;
    private float title_height;
    private List<LawRegulationsMode> modeList;
    private LawRegulationsAdapter adapter;
    private String searchTxt;

    public static LawRegulationsListFragment newInstance() {
        return new LawRegulationsListFragment();
    }

    public LawRegulationsListFragment() {
        presenter = new LawRegulationsPresenter(this);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_law_regulations_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        modeList = new ArrayList<>();
        adapter = new LawRegulationsAdapter(getActivity(), modeList)
                .setOnAdapterClickListener(this);
        listView.setAdapter(adapter);

        smartLayout.setOnRefreshListener(this);

        editSearch.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            public void afterTextChanged(Editable s) {
                String s1 = editSearch.getText().toString();
                if (TextUtils.isEmpty(s1)) {
                    searchTxt = null;
                    imgClear.setVisibility(View.GONE);
                } else imgClear.setVisibility(View.VISIBLE);
            }
        });
        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//EditorInfo.IME_ACTION_SEARCH、EditorInfo.IME_ACTION_SEND等分别对应EditText的imeOptions属性
                    //TODO回车键按下时要执行的操作
                    searchTxt = editSearch.getText().toString();
                    smartLayout.autoRefresh();
                }
                return false;
            }
        });

        asynRun();
    }

    protected void asynRun() {
        Resources res = getResources();
        title_height = res.getDimension(R.dimen.title_height);
        smartLayout.autoRefresh();
    }

    public void onTextSearchClicked() {
        ValueAnimator valueAnimator;
        if (isOpen) {
            isOpen = false;
            searchTxt = null;
            valueAnimator = ValueAnimator.ofFloat(title_height, 0);
            KeyBoardUtils.closeKey(getActivity());
            editSearch.setEnabled(false);
        } else {
            isOpen = true;
            valueAnimator = ValueAnimator.ofFloat(0, title_height);
            editSearch.setEnabled(true);
        }
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value = (float) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams params = layoutSearch.getLayoutParams();
                if (params instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) params;
                    layoutParams.topMargin = (int) value;
                    layoutSearch.setLayoutParams(params);
                }
            }
        });
        valueAnimator.setDuration(300);
        valueAnimator.start();
    }

    @OnClick(R.id.imgClear)
    public void onImgClearClicked() {
        searchTxt = null;
        editSearch.setText("");
    }

    public void onRefresh(RefreshLayout refreshlayout) {
        presenter.getList(searchTxt);
    }

    public void applyError(String t) {
        super.applyError(t);
        smartLayout.finishRefresh();
    }

    public void getListRet(List<LawRegulationsMode> data) {
        smartLayout.finishRefresh();
        modeList.clear();
        if (data != null) modeList.addAll(data);
        adapter.notifyDataSetChanged();
    }

    public void getList2Ret(LawRegulationsMode data) {
        if (data.getModeList().size() == 0) {
            //获取详情
            data.toDetails = true;
            onAdapterClicked2(data);
        } else {
            int i = modeList.indexOf(data);
            if (i >= 0) listView.expandGroup(i);
            adapter.notifyDataSetChanged();
        }
    }

    public void getListDetailsRet(LawRegulationsDetailsMode data) {
        if (data == null) return;
        LawRegulationsDetailsActivity.open(getActivity(), data);
    }

    public void onAdapterClicked(LawRegulationsMode mode) {
        if (mode.toDetails) {
            onAdapterClicked2(mode);
            return;
        }
        List<LawRegulationsMode> list = mode.getModeList();
        if (list.size() == 0) presenter.getList2(mode);
        else {
            int i = modeList.indexOf(mode);
            if (i >= 0) {
                boolean groupExpanded = listView.isGroupExpanded(i);
                if (groupExpanded) {
                    listView.collapseGroup(i);
                } else listView.expandGroup(i);
            }
        }
    }

    public void onAdapterClicked2(LawRegulationsMode mode) {
        presenter.getListDetails(mode.id + "");
    }
}